from flask_restful import Resource, reqparse, marshal_with
from flask_restful_swagger import swagger
from flask import current_app
from com.BookService import BookService
from com.BookModel import BookModel
from com.BookEntity import BookEntity

class BookResources(Resource):
    def __init__(self):
        self.bookService = BookService(current_app)

    @swagger.operation(
        notes='添加图书',
        nickname='add_book',
        responseClass=BookEntity.__name__,
        parameters=[{
            "name": "body",
            "paramType": "json",
            "dataType": BookModel.__name__,
            "required": True,
            "allowMultiple": False,
            "description": "图书信息"
        }, {
            "name": "Authorization",
            "paramType": "cookie",
            "dataType": "string",
            "required": False,
            "allowMultiple": False,
            "description": "Token"
        }],
        responseMessages=[{
            "code": 200,
            "message": "Ok"
        }]
    )
    @marshal_with(BookModel.book_fields)
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('name', type=str, required=True, location='json',help='请输入图书名称')
        parser.add_argument('author', type=str, required=True, location='json',help='请输入图书作者')
        parser.add_argument('price', type=float, required=True, location='json',help='请输入图书名称')
        args = parser.parse_args()
        name = args.get('name')
        author = args.get('author')
        price = args.get('price')
        return self.bookService.add(name, author, price)

    @marshal_with(BookModel.book_list_fields)
    @swagger.operation(
        notes='图书',
        nickname='get_books',
        responseClass=BookEntity.__name__,
        parameters=[{
            "name": "page",
            "paramType": "query",
            "dataType": "integer",
            "required": True,
            "allowMultiple": False,
            "description": "第几页"
        }, {
            "name": "size",
            "paramType": "query",
            "dataType": "integer",
            "required": True,
            "allowMultiple": False,
            "description": "分页大小"
        }, {
            "name": "name",
            "paramType": "query",
            "dataType": "integer",
            "required": False,
            "allowMultiple": False,
            "description": "关键字"
        }, {
            "name": "Authorization",
            "paramType": "cookie",
            "dataType": "string",
            "required": False,
            "allowMultiple": False,
            "description": "Token"
        }],
        responseMessages=[{
            "code": 200,
            "message": "Ok"
        }]
    )
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('page', type=int, required=True)
        parser.add_argument('size', type=int, required=True)
        parser.add_argument('name', type=str, required=False)
        args = parser.parse_args()
        page = args.get('page')
        size = args.get('size')
        name = args.get('name')
        return self.bookService.get(page, size, name)

